package com.woniuxy.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.member.common.MemberResultCode;
import com.woniuxy.member.infra.mapper.CommentInfoMapper;
import com.woniuxy.member.infra.mapper.CommentPicMapper;
import com.woniuxy.member.infra.model.CommentInfo;
import com.woniuxy.member.infra.model.CommentPic;
import com.woniuxy.member.service.CommentInfoService;
import com.woniuxy.member.service.CommentPicService;
import com.woniuxy.member.service.dto.CommentInfoDto;
import com.woniuxy.member.service.param.AddCommentServiceParam;
import com.woniuxy.member.service.param.CommentInfoSearchServiceParam;
import com.woniuxy.member.service.param.CommentInfoSearchServiceParams;
import common.WoniuPage;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评价信息表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2023-07-19
 */
@Service
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoMapper, CommentInfo> implements CommentInfoService {

    @Autowired
    private CommentInfoMapper commentInfoMapper;
    @Autowired
    private CommentPicService commentPicService;
    @Autowired
    private CommentPicMapper commentPicMapper;
    @Override
    public void addComment(AddCommentServiceParam addCommentServiceParam) {
        QueryWrapper<CommentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id",addCommentServiceParam.getStoreId())
                .eq("pay_order_id",addCommentServiceParam.getPayOrderId())
                .eq("member_id",addCommentServiceParam.getMemberId());
        CommentInfo commentInfo = commentInfoMapper.selectOne(queryWrapper);
        if (commentInfo!=null){
            throw new MallException(MemberResultCode.ADDING_COMMENT_FAILED);
        }
        CommentInfo newCommentInfo = BeanUtil.toBean(addCommentServiceParam, CommentInfo.class);
        if (addCommentServiceParam.getFilePath().length==0||addCommentServiceParam.getFilePath()==null){
            newCommentInfo.setPictureFlag(0);
        }else {
            newCommentInfo.setPictureFlag(1);
        }

        newCommentInfo.setCommentDate(new Date());
        save(newCommentInfo);

        for (String path : addCommentServiceParam.getFilePath()) {
            CommentPic commentPic = new CommentPic();
            commentPic.setCommentId(newCommentInfo.getCommentId());
            commentPic.setFilePath(path);
            commentPicService.save(commentPic);
        }
    }

    @Override
    public void delStoreComment(Integer storeId, Integer commentId) {
        CommentInfo commentInfo = getById(commentId);
        if (commentInfo==null){
            throw new MallException(MemberResultCode.COMMENT_DOES_NOT_EXIST);
        }
        if (commentInfo.getStoreId()!=storeId) {
            throw new MallException(MemberResultCode.DELETE_COMMENT_FAILED);
        }
        removeById(commentId);
    }

    @Override
    public void delComment(Integer memberId, Integer storeId, Integer commentId) {
        CommentInfo commentInfo = getById(commentId);
        if (commentInfo==null){
            throw new MallException(MemberResultCode.COMMENT_DOES_NOT_EXIST);
        }
        if (commentInfo.getStoreId()!=storeId||commentInfo.getMemberId()!=memberId) {
            throw new MallException(MemberResultCode.DELETE_COMMENT_FAILED);
        }
        removeById(commentId);
    }

    @Override
    public WoniuPage<CommentInfoDto> getAllScoreCommentInfo(CommentInfoSearchServiceParams commentInfoSearchServiceParams) {
        QueryWrapper<CommentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id",commentInfoSearchServiceParams.getStoreId()).eq("member_id",commentInfoSearchServiceParams.getMemberId());
        if (commentInfoSearchServiceParams.getMinDate()!=null){
            queryWrapper.ge("comment_date",commentInfoSearchServiceParams.getMinDate());
        }
        if (commentInfoSearchServiceParams.getMaxDate()!=null){
            queryWrapper.le("comment_date",commentInfoSearchServiceParams.getMaxDate());
        }
        Page<CommentInfo> commentInfoPage = new Page<>(commentInfoSearchServiceParams.getPageIndex(), commentInfoSearchServiceParams.getPageSize());
        commentInfoMapper.selectPage(commentInfoPage,queryWrapper);
        WoniuPage<CommentInfoDto> woniuPage = new WoniuPage<CommentInfoDto>();
        woniuPage.setTotal(commentInfoPage.getTotal());
        woniuPage.setPages(commentInfoPage.getPages());
        woniuPage.setPageIndex(commentInfoPage.getCurrent());
        woniuPage.setPageSize(commentInfoPage.getSize());
        ArrayList<CommentInfoDto> list = new ArrayList<>();
        for (CommentInfo commentInfo : commentInfoPage.getRecords()) {
            CommentInfoDto commentInfoDto = BeanUtil.toBean(commentInfo, CommentInfoDto.class);
            if (commentInfo.getPictureFlag()==1){
                QueryWrapper<CommentPic> picQueryWrapper = new QueryWrapper<>();
                picQueryWrapper.eq("comment_id",commentInfo.getCommentId());
                List<CommentPic> commentPics = commentPicMapper.selectList(picQueryWrapper);
                List<String> pics = new ArrayList<>();
                for (CommentPic commentPic : commentPics) {
                    pics.add(commentPic.getFilePath());
                }
                commentInfoDto.setCommentPic(pics);
            }
            list.add(commentInfoDto);
        }
        woniuPage.setList(list);
        return woniuPage;
    }

    @Override
    public WoniuPage<CommentInfoDto> getStoreCommentInfo(CommentInfoSearchServiceParam commentInfoSearchServiceParam) {
        QueryWrapper<CommentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id",commentInfoSearchServiceParam.getStoreId());
        if (commentInfoSearchServiceParam.getMinDate()!=null){
            queryWrapper.ge("comment_date",commentInfoSearchServiceParam.getMinDate());
        }
        if (commentInfoSearchServiceParam.getMaxDate()!=null){
            queryWrapper.le("comment_date",commentInfoSearchServiceParam.getMaxDate());
        }
        Page<CommentInfo> commentInfoPage = new Page<>(commentInfoSearchServiceParam.getPageIndex(), commentInfoSearchServiceParam.getPageSize());
        commentInfoMapper.selectPage(commentInfoPage,queryWrapper);
        WoniuPage<CommentInfoDto> woniuPage = new WoniuPage<CommentInfoDto>();
        woniuPage.setTotal(commentInfoPage.getTotal());
        woniuPage.setPages(commentInfoPage.getPages());
        woniuPage.setPageIndex(commentInfoPage.getCurrent());
        woniuPage.setPageSize(commentInfoPage.getSize());
        ArrayList<CommentInfoDto> list = new ArrayList<>();
        for (CommentInfo commentInfo : commentInfoPage.getRecords()) {
            CommentInfoDto commentInfoDto = BeanUtil.toBean(commentInfo, CommentInfoDto.class);
            if (commentInfo.getPictureFlag()==1){
                QueryWrapper<CommentPic> picQueryWrapper = new QueryWrapper<>();
                picQueryWrapper.eq("comment_id",commentInfo.getCommentId());
                List<CommentPic> commentPics = commentPicMapper.selectList(picQueryWrapper);
                List<String> pics = new ArrayList<>();
                for (CommentPic commentPic : commentPics) {
                    pics.add(commentPic.getFilePath());
                }
                commentInfoDto.setCommentPic(pics);
            }
            list.add(commentInfoDto);
        }
        woniuPage.setList(list);
        return woniuPage;
    }
}
